<?php
namespace Anls\Controller;
use Think\Controller;
use Anls\Com\File;
use Anls\Com\Bar;
use Anls\Com\Strategy;
use Anls\Common;
/**
 * 模拟交易
 * @author wsf
 *
 */
class FilterController extends AbstractTradeSupportController{
    
    public function find(){
       
        $this->display('/Report/filter');
    }
    
    public function getFilterData(){
        // 开始日期
        $backShift = I('backShift',7);
        //
        $beginDate = I('beginDate');
        $fileList = $this->redis->zRange(Common\Redis::$TRADE_CODE_KEY, 0, -1);
        
        $fileListCnt = count($fileList);
        $rst = array();
        for($fi=0;$fi<$fileListCnt;$fi++){
            // 获取文件名
            $fileName = $fileList[$fi];
            $prev_ma5 = -1;
            $prev_ma10 = -1;
            $prev_date = '';
            $_backShift = $backShift;
            $jumpDay = 0;
            $shiftBar = 1;
            for($i=0;$i<$_backShift;$i++){
        
                $_val = '-'.$i.' day';
                $_date = date('Y/m/d',strtotime($_val));
        
                $week = date('w',strtotime($_date));
                if($week == 6 || $week == 0) {
                    $_backShift ++;
                    $jumpDay ++;
                    continue;
                }
                /*
                if($fileName == "000005"){
                    echo $prev_ma5.'__'.$i.'-'.$_date.'__'.$_data.'</br>';
                }*/
                $_key = 'kdata:'.$fileName.':1day:'.$_date;
                if(!$this->redis_k_db->exists($_key)){
                    $shiftBar++;
                    continue;
                }
                $_data =  $this->redis_k_db->get($_key);
                
                if($_data == null) {
                    $shiftBar++;
                    continue;
                }
                $_data = json_decode($_data,true);
                $ma5 = $_data['MA_5'];
                $ma10 = $_data['MA_10'];
                if($prev_ma5 == -1 && $ma5<$ma10) break;
                if($ma5 > $ma10){
                    $prev_date = $_date;
                    $prev_ma5 = $ma5;
                    $prev_ma10 = $ma10;
                    $shiftBar++;
                } else {
                    if($ma5 < $ma10){
                        $shiftBar--;
                    }
                    // 获取分析
                    $anlsData = $this->redis->get(Common\Redis::$TRADE_ANLS_KEY.$fileName);
                    $anlsData = json_decode($anlsData,true);
                    $rstArrData = array('code'=>$fileName
                        ,'shift'=>($shiftBar)
                        ,'date'=>$prev_date
                        ,'loseLimit.cur'=>$anlsData['loseLimit']['cur']
                        ,'highWinLt.cur'=>$anlsData['highWinLt']['cur']
                    );
                    array_push($rst,$rstArrData);
                    break;
                }
                
            }
           // if($fileName == "000005")break;
        }
        echo json_encode($rst);
    }
    /*
    $text = '';
    for($i=0;$i<count($rst);$i++){
        $text .= $rst[$i]['code'].'--'.$rst[$i]['shift'].'--'.$rst[$i]['date'];
        $text .= '--loseLimit.cur:'.$rst[$i]['loseLimit.cur'].'--highWinLt.cur:'.$rst[$i]['highWinLt.cur'];
        $text .= '</br>';
    }
    */
}